home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d21 / dvglue.arc / DEMO.C < prev    next >
C/C++ Source or Header  |  1990-01-09  |  26KB  |  740 lines

  1. /*
  2. **      DEMO.C - DESQview demonstration program.
  3. **      IBM Personal Computer - DESQview C Interface Demo.
  4. **    Written for Borland Turbo C v1.5.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <dir.h>
  9. #include <bios.h>
  10. #include <io.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13.  
  14. #include "tvapi.h"      /* types and function prototypes */
  15. #include "tvstream.h"
  16.  
  17. #if __TURBOC__ != 1 && __TURBOC__ != 0x0100
  18. unsigned _heaplen = 10240 ;  /* only need 10K heap */
  19. #endif
  20.  
  21. #define VERSION_STR "0.91"
  22.  
  23. /*===============================================================*/
  24.  
  25. #define STDERR  2
  26.  
  27. #define WR_STRC(f,s)    write(f,s,sizeof(s)-1)  /* string constant */
  28. #define SO_STRC(s)      WR_STRC(STDOUT,s)   /* string con to STDOUT */
  29. #define SE_STRC(s)      WR_STRC(STDERR,s)   /* string con to STDERR */
  30.  
  31. void write_int(int i)
  32. {
  33.    char a[9] ;
  34.  
  35.    itoa(i,a,10) ;
  36.    TVwin_swrite(NIL,a) ;
  37. }
  38.  
  39. /*===============================================================*/
  40.  
  41. void stream_test(void)
  42. {
  43.    int row, col ;
  44.    char old_title[24] ;
  45.    int old_frattr ;
  46.    int org_row, org_col ;
  47.    char buffer[10] ;
  48.    int i ;
  49.  
  50.    TVwin_resize(NIL,13,66) ;
  51.    TVwin_move(NIL,7,5) ;
  52.    TVwin_cursor(NIL,0,0) ;
  53.    TVqry_origin(NIL,&org_row,&org_col) ;
  54.    TVwin_origin(NIL,org_row,org_col) ;
  55.    TVqry_title(NIL,old_title,sizeof(old_title)) ;
  56.    TVwin_title(NIL,"DV-GLUE v" VERSION_STR) ;
  57.    old_frattr = TVqry_frattr(NIL) ;
  58.    TVwin_frattr(NIL,9) ;
  59.    TVwin_clear(NIL) ;
  60.    TVwin_redraw(NIL) ;
  61.    TVwin_printf(NIL,"Window #%d, old name was %s, origin (%d,%d)\r\n", DVappnum(), old_title, org_row,org_col) ;
  62.    TVqry_cursor(NIL,&row,&col) ;
  63.    TVwin_printf(NIL,"Cursor position is (%d,%d)\r\n",row,col) ;
  64.    TVqry_position(NIL,&row,&col) ;
  65.    TVwin_printf(NIL,"Window is at (%d,%d) ",row,col) ;
  66.    TVqry_size(NIL,&row,&col) ;
  67.    TVwin_printf(NIL,"and is %dx%d",row,col) ;
  68.    TVqry_lsize(NIL,&row,&col) ;
  69.    TVwin_printf(NIL,"/%dx%d in phys/logical size\r\n",row,col) ;
  70.    TVwin_printf(NIL,"The current output attribute is %d\r\n",TVqry_attr(NIL)) ;
  71.    TVwin_printf(NIL,"The old frame attribute was %d\r\n",old_frattr) ;
  72.    TVwin_printf(NIL,"Control code processing is %s",TVqry_ctrl(NIL)?"on":"off") ;
  73.    TVwin_printf(NIL," and we are \r\n  %s when writing\r\n",TVqry_leave(NIL)?
  74.                                        "leaving attributes untouched":"changing attributes") ;
  75.    TVwin_printf(NIL,"Using %s attributes\r\n",TVqry_logattr(NIL)?"logical":"physical") ;
  76.    TVwin_hcur(NIL) ;
  77.    TVwin_atread(NIL,FALSE) ;
  78.    TVqry_cursor(NIL,&row,&col) ;
  79.    TVwin_cursor(NIL,0,0) ;
  80.    TVwin_nread(NIL,(void *)buffer,sizeof(buffer)) ;
  81.    TVwin_cursor(NIL,row,col) ;
  82.    TVwin_printf(NIL,"First method read '%s' from screen\n",buffer) ;
  83.    TVwin_cursor(NIL,0,0) ;
  84.    TVwin_read(NIL,(void *)buffer,sizeof(buffer)) ;
  85.    TVwin_gotoxy(NIL,col,row+1) ;
  86.    TVwin_printf(NIL,"Second method read '%s' from screen\n",buffer) ;
  87.  
  88.    TVwin_swrite(NIL,"\r\nPress a key to hide window, then another to unhide it....");
  89.    TVwin_hcur(NIL) ;
  90.    bioskey(0) ;
  91.    TVwin_hide(NIL) ;
  92.    bioskey(0) ;
  93.    TVwin_unhide(NIL) ;
  94.    TVwin_swrite(NIL,"\r\nPress a key to continue..." ) ;
  95.    TVwin_hcur(NIL) ;
  96.    bioskey(0) ;
  97.    TVqry_size(NIL,&row,&col) ;
  98.    for (i = 0 ; i < col ; i++)
  99.       {
  100.       TVsleep(5) ;
  101.       TVwin_scroll(NIL,0,0,row,col,SCRL_LEFT) ;
  102.       }
  103. }
  104.  
  105. /*===============================================================*/
  106.  
  107. void timer_test(void)
  108. {
  109.    OBJECT k, t, w ;
  110.    int left, elapsed ;
  111.  
  112.    w = TVwin_new(NIL,5,46) ;
  113.    TVwin_clear(w) ;
  114.    TVposwin(w,NIL,PW_HCENTER|PW_VCENTER) ;
  115.    TVwin_cursor(w,0,0) ;
  116.    TVwin_origin(w,0,0) ;
  117.    TVwin_top(w) ;
  118.    TVwin_printf(w,"Press a few keys during the next ten seconds:"
  119.                   "\r\n(this still doesn't work)") ;
  120.    k = TVkbd_new() ;
  121.    TVkbd_open(k,w) ;  /* before connecting new kbd to window */
  122.    TVkbd_setflags(k,0x2004|KBD_ACTIVE) ;
  123.    t = TVtimer_new() ;
  124.    TVtimer_begin(t,1000) ;  /* 10 seconds */
  125.    while ((left = (int) TVtimer_len(t)) > 0)
  126.       {
  127.       elapsed = (int) TVtimer_elapsed(t) ;
  128.       TVwin_cursor(w,3,0) ;
  129.       TVwin_printf(w,"Time left: %2d.%02d  Keys pressed: %d\r\n  Elapsed: %2d.%02d",
  130.                        left/100, left%100, TVkbd_messages(k), elapsed/100,
  131.                        elapsed%100) ;
  132.       }
  133.    TVtimer_free(t) ;
  134.    TVkbd_clear(k) ;  /* get rid of typeahead */
  135.    TVwin_cursor(w,0,0) ;
  136.    TVwin_printf(w,"%5d keys left after clearing typeahead     \r\n",TVkbd_messages(k));
  137.    while (bioskey(1))  /* brute-force keyboard clear */
  138.       bioskey(0) ;
  139.    TVsleep(100) ;
  140.    TVkbd_close(k) ;
  141.    TVkbd_free(k) ;
  142.    TVwin_free(w) ;
  143.    TVkbd_setflags(NIL,KBD_ACTIVE) ;   /* open and make active again */
  144. }
  145.  
  146. /*===============================================================*/
  147.  
  148. void far notify_func(void)
  149. {
  150.    static NOTIFY_MSG msg ; /* must be static to be in DS since SS != DS */
  151.                            /* in this routine even in small data models */
  152.    int i, len ;
  153.  
  154.    /* at this point, we are on the private stack, not our own */
  155.    /* switching to our prior stack causes a crash */
  156.    /* NOTE: can't use TVwin_printf unless we can ensure that it isn't used in */
  157.    /* any routines which might be interrupted for this notification */
  158.    /* also need to be careful with any routines that need SS == DS in small */
  159.    /* data models */
  160.    while (TVmbx_size(NIL))
  161.       {
  162.       len = TVreadmail(NIL,(char *)&msg,sizeof(msg)) ;
  163.       TVwin_swrite(NIL,"\r\n                                                                 \r") ;
  164.       switch (msg.event-MS_NOTIFY)
  165.          {
  166.          case TV_HMOVE:
  167.          case TV_VMOVE:
  168.                   TVwin_swrite(NIL,"Hey, don't move me!  Now at ") ;
  169.                   write_int(msg.msg.movement.row) ;
  170.                   TVwin_swrite(NIL,",") ;
  171.                   write_int(msg.msg.movement.col) ;
  172.                   break ;
  173.          case TV_HSIZE:
  174.          case TV_VSIZE:
  175.                   TVwin_swrite(NIL,"I liked my previous size!  New size ") ;
  176.                   write_int(msg.msg.resizing.rows) ;
  177.                   TVwin_swrite(NIL," by ") ;
  178.                   write_int(msg.msg.resizing.cols) ;
  179.                   break ;
  180.          case TV_HSCROLL:
  181.                   TVwin_swrite(NIL,"You scrolled me ") ;
  182.                   if (msg.msg.scrolling.direction > 0)
  183.                      TVwin_swrite(NIL,"right") ;
  184.                   else if (msg.msg.scrolling.direction < 0)
  185.                      TVwin_swrite(NIL,"left") ;
  186.                   else
  187.                      TVwin_swrite(NIL,"[done]") ;
  188.                   break ;
  189.          case TV_VSCROLL:
  190.                   TVwin_swrite(NIL,"You scrolled me ") ;
  191.                   if (msg.msg.scrolling.direction > 0)
  192.                      TVwin_swrite(NIL,"down") ;
  193.                   else if (msg.msg.scrolling.direction < 0)
  194.                      TVwin_swrite(NIL,"up") ;
  195.                   else
  196.                      TVwin_swrite(NIL,"[done]") ;
  197.                   break ;
  198.          case TV_CLOSE:
  199.                   TVwin_swrite(NIL,"You can't close me yet!  Next time WILL close me") ;
  200.                   TVwin_cancel(NIL,TV_CLOSE) ;
  201.                   break ;
  202.          case TV_HIDE:
  203.                   TVwin_swrite(NIL,"You hid me!") ;
  204.                   break ;
  205.          case TV_HELP:
  206.                   TVwin_swrite(NIL,"Sorry, no help available.  Mouse at (") ;
  207.                   write_int(msg.msg.help.mouse_row) ;
  208.                   TVwin_swrite(NIL,",") ;
  209.                   write_int(msg.msg.help.mouse_col) ;
  210.                   TVwin_swrite(NIL,")") ;
  211.                   break ;
  212.          case TV_COLORS:
  213.                   TVwin_swrite(NIL,"Was the color changed?") ;
  214.                   break ;
  215.          case TV_SWITCHEDTO:
  216.                   TVwin_swrite(NIL,"Thanks for making me active.") ;
  217.                   break ;
  218.          case TV_SWITCHEDAWAY:
  219.                   TVwin_swrite(NIL,"Hey, I want to stay active!") ;
  220.                   break ;
  221.          case TV_VIDEOMODE:
  222.                   TVwin_swrite(NIL,"New video mode is ") ;
  223.                   write_int(msg.msg.video_mode) ;
  224.                   break ;
  225.          case TV_SCISSORS_CUT:
  226.                   TVwin_swrite(NIL,"Sorry, don't know how to cut (") ;
  227.                   write_int(msg.msg.scissors.up_left_row) ;
  228.                   TVwin_swrite(NIL,",") ;
  229.                   write_int(msg.msg.scissors.up_left_col) ;
  230.                   TVwin_swrite(NIL,") to (") ;
  231.                   write_int(msg.msg.scissors.up_left_row + msg.msg.scissors.height - 1) ;
  232.                   TVwin_swrite(NIL,",") ;
  233.                   write_int(msg.msg.scissors.up_left_col + msg.msg.scissors.width - 1) ;
  234.                   TVwin_swrite(NIL,")") ;
  235.                   break ;
  236.          case TV_SCISSORS_COPY:
  237.                   TVwin_swrite(NIL,"Sorry, don't know how to copy (") ;
  238.                   write_int(msg.msg.scissors.up_left_row) ;
  239.                   TVwin_swrite(NIL,",") ;
  240.                   write_int(msg.msg.scissors.up_left_col) ;
  241.                   TVwin_swrite(NIL,") to (") ;
  242.                   write_int(msg.msg.scissors.up_left_row + msg.msg.scissors.height - 1) ;
  243.                   TVwin_swrite(NIL,",") ;
  244.                   write_int(msg.msg.scissors.up_left_col + msg.msg.scissors.width - 1) ;
  245.                   TVwin_swrite(NIL,")") ;
  246.                   break ;
  247.          case TV_SCISSORS_PASTE:
  248.                   TVwin_swrite(NIL,"Sorry, don't know how to paste (") ;
  249.                   write_int(msg.msg.scissors.up_left_row) ;
  250.                   TVwin_swrite(NIL,",") ;
  251.                   write_int(msg.msg.scissors.up_left_col) ;
  252.                   TVwin_swrite(NIL,") to (") ;
  253.                   write_int(msg.msg.scissors.up_left_row + msg.msg.scissors.height - 1) ;
  254.                   TVwin_swrite(NIL,",") ;
  255.                   write_int(msg.msg.scissors.up_left_col + msg.msg.scissors.width - 1) ;
  256.                   TVwin_swrite(NIL,")") ;
  257.                   break ;
  258.          case TV_MAINMENU:
  259.                   TVwin_swrite(NIL,"Main menu popped up") ;
  260.                   break ;
  261.          case TV_MENU_END:
  262.                   TVwin_swrite(NIL,"Main menu put away") ;
  263.                   break ;
  264.          default: TVwin_swrite(NIL,"Unknown event ") ;
  265.                   write_int(msg.event) ;
  266.                   TVwin_swrite(NIL,"!  What happened?") ;
  267.                   break ;
  268.          }
  269.       TVwin_swrite(NIL," [") ;
  270.       for (i = 0 ; i < len ; i++)
  271.          {
  272.          TVputchar(NIL,"0123456789ABCDEF"[((BYTE)(((char *)&msg)[i])) >> 4],15) ;
  273.          TVputchar(NIL,"0123456789ABCDEF"[((BYTE)(((char *)&msg)[i])) & 0x0F],15) ;
  274.          }
  275.       TVwin_swrite(NIL,"]") ;
  276.       }
  277. }
  278.  
  279. void notify_test(void)
  280. {
  281.    int i ;
  282.  
  283.    TVwin_async(NIL,notify_func) ;
  284.    for (i = TV_HMOVE ; i <= TV_SWITCH ; i++)
  285.       {
  286.       TVwin_allow(NIL,i) ;
  287.       TVwin_notify(NIL,i) ;
  288.       }
  289.    TVwin_disallow(NIL,TV_QUIT) ;
  290.    TVwin_clear(NIL) ;
  291.    TVwin_cursor(NIL,0,0) ;
  292.    TVwin_origin(NIL,0,0) ;
  293.    TVwin_swrite(NIL,"Just try to do something to me! (including closing me)") ;
  294.    TVwin_cursor(NIL,4,0) ;
  295.    TVwin_swrite(NIL,"Press a key when done....") ;
  296.    TVwin_hcur(NIL) ;
  297.    bioskey(0) ;
  298.    for (i = TV_HMOVE ; i <= TV_SWITCH ; i++)
  299.       TVwin_cancel(NIL,i) ;  /* cancel notification */
  300.    TVwin_allow(NIL,TV_QUIT) ;
  301.    TVwin_clear(NIL) ;
  302. }
  303.  
  304. /*===============================================================*/
  305.  
  306. void pointer_test(void)
  307. {
  308.    OBJECT p ;
  309.    int i, rows, cols ;
  310.  
  311.    TVwin_title(NIL,"Mouse Demo") ;
  312.    TVwin_clear(NIL) ;
  313.    TVwin_origin(NIL,0,0) ;
  314.    TVwin_redraw(NIL) ;
  315.    p = TVptr_new() ;
  316.    TVptr_open(p,NIL) ;
  317.    TVptr_icon(p,'+') ;
  318.    TVqry_size(NIL,&rows,&cols) ;
  319.    TVwin_disallow(NIL,TV_HSIZE) ;
  320.    TVwin_swrite(NIL,"Please make sure mouse pointer is visible if you are\r\n"
  321.                     "using a keyboard mouse, then press a key\r\n" ) ;
  322.    TVwin_hcur(NIL) ;
  323.    bioskey(0) ;
  324.    TVwin_swrite(NIL,"\r\nMoving mouse pointer to screen origin") ;
  325.    TVqry_position(NIL,&rows,&cols) ;
  326.    TVptr_goto(p,-rows,-cols) ;
  327.    TVsleep(150) ;
  328.    TVwin_swrite(NIL,"\r\nMoving mouse pointer to positions relative to window\r\n");
  329.    for (i = 0 ; i < rows ; i++)
  330.       {
  331.       TVptr_goto(p,i,i/2+1) ;
  332.       TVsleep(25) ;
  333.       }
  334.    TVwin_allow(NIL,TV_HSIZE) ;
  335.    TVptr_close(p) ;
  336.    TVptr_free(p) ;
  337.    if (TVisobj(p))
  338.       TVerrormsg(0,"Error--Pointer wasn't freed!",1,0,0,0) ;
  339.    else
  340.       {
  341.       TVwin_swrite(NIL,"\r\nPress a key to continue....") ;
  342.       TVwin_hcur(NIL) ;
  343.       bioskey(0) ;
  344.       }
  345.    TVwin_clear(NIL) ;
  346. }
  347.  
  348. /*===============================================================*/
  349.  
  350. void multiwindow_test(void)
  351. {
  352.    OBJECT win1 = TVwin_new(NIL,10,15) ;
  353.    OBJECT win2 = TVwin_new(NIL,5,20) ;
  354.    OBJECT win3 = TVwin_new(NIL,8,30) ;
  355.  
  356.    TVwin_move(win1,6,60) ;
  357.    TVwin_title(win1,"Window 1") ;
  358.    TVwin_redraw(win1) ;
  359.    TVwin_move(win2,1,52) ;
  360.    TVwin_title(win2,"Window 2") ;
  361.    TVwin_redraw(win2) ;
  362.    TVwin_move(win3,4,40) ;
  363.    TVwin_title(win3,"Window 3") ;
  364.    TVwin_redraw(win3) ;
  365.    TVwin_title(NIL,"Multiwindow Demo") ;
  366.    TVwin_redraw(NIL) ;
  367.    TVsleep(150) ;
  368.    TVwin_reorder(win1,OBJSEG(win3),OBJSEG(win2),OBJSEG(TVmywindow()),OBJSEG(win1),0);
  369.    TVwin_swrite(win1,"Window 1 on top") ;
  370.    TVsleep(150) ;
  371.    TVwin_reorder(win3,OBJSEG(win2),OBJSEG(win1),OBJSEG(TVmywindow()),OBJSEG(win3),0);
  372.    TVwin_swrite(win3,"Window 3 on top") ;
  373.    TVsleep(150) ;
  374.    TVwin_reorder(win2,OBJSEG(win3),OBJSEG(TVmywindow()),OBJSEG(win1),OBJSEG(win2),0);
  375.    TVwin_swrite(win2,"Window 2 on top") ;
  376.    TVsleep(150) ;
  377.    TVwin_reorder(NIL,OBJSEG(win2),OBJSEG(win3),OBJSEG(win1),OBJSEG(TVmywindow()),0);
  378.    TVwin_swrite(NIL,"\r\nWindow 0 on top") ;
  379.    TVsleep(100) ;
  380.    TVwin_top(win3) ;
  381.    TVwin_swrite(win3,"\n\rWindow 3 on top again") ;
  382.    TVsleep(100) ;
  383.    TVwin_topsys(win2) ;
  384.    TVwin_swrite(win2,"\n\rWindow 2 now topmost" ) ;
  385.    TVsleep(100) ;
  386.    TVwin_top(NIL) ;
  387.    TVwin_swrite(NIL,"\n\rBack to window 0" ) ;
  388.    TVsleep(100) ;
  389.    TVkbd_open(NIL,NIL) ;  /* make sure keyboard is attached to main window */
  390.    TVwin_swrite(NIL,"\r\nPlease open another window, then press a key....") ;
  391.    TVwin_hcur(NIL) ;
  392.    bioskey(0) ;
  393.    TVwin_top(NIL) ;
  394.    TVwin_swrite(NIL,"\n\rAbout to hide and then unhide all my windows....") ;
  395.    TVsleep(150) ;
  396.    TVapp_hide(NIL) ;    /* also puts us in background, */
  397.    TVsleep(100) ;
  398.    TVapp_show(NIL) ;
  399.    TVsleep(10) ;
  400.    TVapp_gofore(NIL) ;  /* so have to force ourself into foreground */
  401.    TVwin_swrite(NIL,"\r\nDone!") ;
  402.    TVwin_redraw(NIL) ;
  403.    TVsleep(50) ;
  404.    TVwin_swrite(NIL,"\r\n\nI am about to put myself in the background, and"
  405.                     "\r\nthen back into the foreground") ;
  406.    TVsleep(200) ;
  407.    TVapp_goback(NIL) ;
  408.    TVsleep(100) ;
  409.    TVapp_gofore(NIL) ;
  410.    TVwin_swrite(NIL,"\r\nDone!") ;
  411.    TVsleep(50) ;
  412.    TVwin_free(win3) ;
  413.    TVsleep(50) ;
  414.    TVwin_free(win2) ;
  415.    TVsleep(50) ;
  416.    TVwin_free(win1) ;
  417.    TVsleep(50) ;
  418.    TVwin_frattr(NIL,0x70) ;  /* reverse video */
  419.    TVwin_redraw(NIL) ;       /* make sure physical screen is updated */
  420.  
  421.    /* it seems that writing to multiple windows requires reconnecting the
  422.       keyboard to the window you really want to talk to .... */
  423.    TVkbd_clrflags(NIL,KBD_ACTIVE) ;
  424.    TVkbd_close(NIL) ;  /* disconnect keyboard from window */
  425.    TVkbd_open(NIL,NIL) ;
  426.    TVkbd_setflags(NIL,KBD_ACTIVE) ; /* reconnect to main window and make active */
  427.    TVkbd_clear(NIL) ;
  428.  
  429.    TVwin_swrite(NIL,"\r\nPress a key to continue....") ;
  430.    TVwin_hcur(NIL) ;
  431.    bioskey(0) ;
  432. }
  433.  
  434. /*===============================================================*/
  435.  
  436. void subtask1(int parent)
  437. {
  438.    char kbd_input[16] ;
  439.    char mbx_input[32] ;
  440.    int mbx_status ;
  441.    OBJECT mbx ;
  442.  
  443.    do {
  444.       TVkbd_read(NIL,kbd_input,sizeof(kbd_input)) ;
  445.       if (kbd_input[0] != 0x1B)
  446.          TVwin_swrite(NIL,kbd_input) ;
  447.       } while (kbd_input[0] != 0x1B) ;
  448.    TVwin_clear(NIL) ;
  449.    do {
  450.       TVreadmail(NIL,mbx_input, sizeof(mbx_input)) ;
  451.       mbx_status = (int) TVmbx_status(NIL) ;
  452.       if (mbx_input[0] == '\0')
  453.          TVwin_swrite(NIL,"\r\nThat's all, folks!" ) ;
  454.       else
  455.          {
  456.          TVwin_swrite(NIL, "\r\nRead: '") ;
  457.          TVwin_swrite(NIL, mbx_input) ;
  458.          TVwin_swrite(NIL, "' with status ") ;
  459.          write_int(mbx_status) ;
  460.          if (OBJSEG(TVmbx_sender(NIL)) == parent)
  461.             TVwin_swrite(NIL," from parent") ;
  462.          else
  463.             TVwin_swrite(NIL," from a stranger") ;
  464.          }
  465.       } while (mbx_input[0] != '\0') ;
  466.    TVsleep(250) ;
  467.    TVwin_clear(NIL) ;
  468.    TVwin_swrite(NIL,"Test of named mailboxes:\r\n") ;
  469.    mbx = TVmbx_new() ;
  470.    TVmbx_open(mbx);
  471.    TVmbx_name(mbx,"Test Mailbox") ;
  472.    TVmbx_clear(mbx) ;
  473.    TVmbx_write(TVmbxof(MK_OBJ(parent)),FALSE,0,"Ready",5) ;
  474.    TVreadmail(mbx,mbx_input,sizeof(mbx_input)) ;
  475.    TVwin_swrite(NIL,"Got '") ;
  476.    TVwin_swrite(NIL,mbx_input) ;
  477.    TVwin_swrite(NIL,"'") ;
  478.    TVsleep(150) ;  /* don't ACK right away */
  479.    TVmbx_write(TVmbxof(MK_OBJ(parent)),FALSE,0,"Done",4) ;
  480.    TVreadmail(NIL,mbx_input,sizeof(mbx_input)) ;
  481.    TVmbx_close(mbx) ;
  482.    TVmbx_free(mbx) ;
  483. }
  484.  
  485. void subtask2(int parent)
  486. {
  487.    int row = 0, col = 0 ;
  488.    int drow, dcol ;
  489.    int rows, cols ;
  490.    int rep ;
  491.  
  492.    (void) parent ;  /* get rid of TurboC's warning about unused parameters */
  493.    TVwin_cursor(NIL,0,0) ;
  494.    TVwin_hcur(NIL) ;
  495.    drow = 1 ;
  496.    dcol = 1 ;
  497.    for (rep = 0 ; rep < 30 ; rep++)
  498.       do {
  499.          if (TVmbx_size(NIL) != 0)
  500.             {
  501.             TVmbx_clear(NIL) ;  /* discard messages, only used to notify us */
  502.             TVstoptask(NIL) ;   /* and suspend ourselves */
  503.             }
  504.          TVwin_cursor(NIL,row,col) ;
  505.          TVwin_swrite(NIL," ") ;
  506.          TVqry_size(NIL, &rows, &cols) ;
  507.          if (row < 0 || row >= rows-1)
  508.             drow = -drow ;
  509.          if (col < 0 || col >= cols-1)
  510.             dcol = -dcol ;
  511.          row += drow ;
  512.          col += dcol ;
  513.          TVwin_cursor(NIL,row,col) ;
  514.          TVwin_swrite(NIL,"+") ;
  515.          TVsleep(10) ;
  516.          } while (row != 0 || col != 0) ;
  517. }
  518.  
  519. void stripCRLF(char *buf)
  520. {
  521.    char *s = buf + strlen(buf) -1 ;
  522.  
  523.    while (s >= buf && (*s == '\r' || *s == '\n'))
  524.       s-- ;
  525.    s[1] = '\0' ;
  526. }
  527.  
  528.  
  529. void fork_test(void)
  530. {
  531.    char key, keys[2], prog[30], arg1[30] ;
  532.    OBJECT task1, task2, new_task ;
  533.    int count = 0 ;
  534.    char *progname ;
  535.    char mail[10] ;
  536.  
  537.    TVwin_clear(NIL) ;
  538.    TVwin_cursor(NIL,0,0) ;
  539.    task1 = TVnew_task(NIL,"Echo keys",0,39,3,40,NULL,1000,subtask1) ;
  540.    if (task1 == NIL)
  541.       {
  542.       TVerrormsg(0,"Insufficient memory or other error--Press ESC",1,0,0,0);
  543.       return ;
  544.       }
  545.    TVsleep(50) ;
  546.    task2 = TVnew_task(NIL,"Another task",14,0,10,78,NULL,1000,subtask2) ;
  547.    TVwin_swrite(NIL,"Type characters to echo in other window, ESC to quit") ;
  548.    do {
  549.       key = bioskey(0) ;
  550.       keys[0] = key ;
  551.       TVkbd_write(TVkbdof(task1),keys,1,0) ;
  552.       } while (key != 0x1B) ;
  553.    TVsleep(100) ;  /* allow time for other window to clear */
  554.    TVwin_swrite(NIL,"\r\nType strings to echo in other window, press just RETURN to quit\r\n") ;
  555.    TVwin_hcur(NIL) ;
  556.    do {
  557.       fgets(arg1, sizeof(arg1), stdin) ;
  558.       stripCRLF(arg1) ;
  559.       TVmbx_write(TVmbxof(task1),FALSE,count++,arg1, strlen(arg1)) ;
  560.    } while (strlen(arg1) > 0) ;
  561.  
  562.    TVmbx_clear(NIL) ;
  563.    TVwin_swrite(NIL,"\r\n\r\nTesting named mailboxes\r\n") ;
  564.    TVreadmail(NIL,mail,sizeof(mail)) ;  /* wait for other task to set up mbx */
  565.    TVwin_swrite(NIL,"sending... " ) ;
  566.    TVmbx_write(TVmbx_find("Test Mailbox"),FALSE,0,"Test successful",15) ;
  567.    TVreadmail(NIL,mail,sizeof(mail)) ;  /* wait for ACK */
  568.    TVwin_swrite(NIL,"got ACK: '") ;
  569.    TVwin_swrite(NIL,mail) ;
  570.    TVwin_swrite(NIL,"'") ;
  571.    TVmbx_write(TVmbxof(task1),FALSE,0,"Quit",4) ;
  572.  
  573.    TVsleep(100) ;  /* allow time for other window to close */
  574.    TVwin_swrite(NIL,"\r\n\r\nPress a key to continue....") ;
  575.    TVwin_hcur(NIL) ;
  576.    bioskey(0) ;
  577.    TVsendmail(TVmbxof(task2),"",0) ;  /* would like to do  TVstoptask(task2) */
  578.                                       /* but DV 2.00 only allows suspending */
  579.                                       /* the current process, not others */
  580.    TVwin_swrite(NIL,"\r\nThe 'bouncing' plus sign in the other window should"
  581.                     "\r\nhave stopped moving.  Press a key....") ;
  582.    TVwin_hcur(NIL) ;
  583.    bioskey(0) ;
  584.    TVstarttask(task2) ;
  585.    TVwin_swrite(NIL,"\r\nIt should have started moving again.  Press a key....") ;
  586.    TVwin_hcur(NIL) ;
  587.    bioskey(0) ;
  588.    TVfreetask(task1) ;
  589.    TVfreetask(task2) ;  /* make sure the subtasks are both terminated */
  590.    TVwin_swrite(NIL,"\r\nOK, now let's load a user program.\r\nProgram's name, including extension: ");
  591.    TVwin_hcur(NIL) ;
  592.    fgets(prog,sizeof(prog),stdin) ;
  593.    stripCRLF(prog) ;
  594.    TVwin_swrite(NIL,"\r\nThe program's argument: " ) ;
  595.    TVwin_hcur(NIL) ;
  596.    fgets(arg1,sizeof(arg1),stdin) ;
  597.    stripCRLF(arg1) ;
  598.    progname = searchpath(prog) ;
  599.    if (progname)
  600.       {
  601.       TVwin_printf(NIL,"\nInvoking '%s %s'\n",progname,arg1) ;
  602.       new_task = TVapp_new(NIL,0,0,20,78,progname,progname,arg1,NULL) ;
  603.       if (new_task != NIL)
  604.          {
  605.          TVwin_swrite(NIL,"\r\nExperiment with switching between this program and "
  606.                           "\r\nthe one you just invoked.  Press a key when done") ;
  607.          TVwin_hcur(NIL) ;
  608.          bioskey(0) ;
  609.          TVfreetask(new_task) ;
  610.          }
  611.       else
  612.          TVerrormsg(0,"Error loading--not enough memory?  ESC to continue",1,0,0,0) ;
  613.       }
  614.    else
  615.       TVwin_printf(NIL,"\n\n\nCouldn't find %s!\n", prog ) ;
  616. }
  617.  
  618. /*===============================================================*/
  619.  
  620. void far second_level_interrupt(void)
  621. {
  622.    /* note that in this routine, DS/ES/SS are unknown */
  623.    TVsound(1000,10) ;
  624. }
  625.  
  626. void interrupt_test(void)
  627. {
  628.    WORD bit = TVgetbit(second_level_interrupt) ;
  629.  
  630.    TVwin_swrite(NIL,"\r\n\r\nPress a key to test second level interrupts") ;
  631.    TVwin_hcur(NIL) ;
  632.    bioskey(0) ;
  633.    TVwin_swrite(NIL,"\r\n\r\nBeeping....") ;
  634.    TVsetbit(bit) ;
  635.    TVsleep(100) ;
  636.    TVfreebit(bit) ;
  637.    TVwin_swrite(NIL,"Done!\r\n") ;
  638. }
  639.  
  640. /*===============================================================*/
  641.  
  642. void objectq_test(void)
  643. {
  644.    OBJECT p = TVptr_new() ;
  645.    OBJECT obj ;
  646.    char buffer[100] ;
  647.    int i, count ;
  648.  
  649.    TVobq_open(NIL) ;
  650.    TVkbd_clear(NIL) ;
  651.    TVobq_add(NIL,TVmykbd()) ;
  652.    TVptr_open(p,NIL) ;
  653.    TVptr_setflags(p,1) ;
  654.    TVptr_erase(p) ;
  655.    TVobq_add(NIL,p) ;
  656.    TVwin_clear(NIL) ;
  657.    TVwin_title(NIL,"OBJECTQ demo") ;
  658.    TVwin_cursor(NIL,0,0) ;
  659.    TVwin_origin(NIL,0,0) ;
  660.    TVwin_swrite(NIL,"Waiting for input from either the keyboard or a mouse\r\n") ;
  661.    TVwin_swrite(NIL,"(a keyboard mouse doesn't count as mouse input)") ;
  662.    obj = TVobq_read(NIL) ;
  663.    count = TVptr_read(obj,buffer,sizeof(buffer)) ;
  664.    TVwin_swrite(NIL,"\r\nGot: ") ;
  665.    for (i = 0 ; i < count ; i++)
  666.       TVwin_printf(NIL,"%02x ",buffer[i]) ;
  667.    TVobq_remove(NIL,p) ;  /* don't take input from mouse anymore */
  668.    TVptr_close(p) ;       /* don't need the object, so free it */
  669.    TVptr_free(p) ;
  670.    TVsleep(100) ;
  671.    TVobq_close(NIL) ;
  672. }
  673.  
  674. /*===============================================================*/
  675.  
  676. void field_test(void)
  677. {
  678.    char buffer[50] ;
  679.    BYTE *fields ;
  680.    int status, len ;
  681.  
  682.    TVwin_title(NIL,"Field Demo") ;
  683.    TVwin_clear(NIL) ;
  684.    TVwin_gotoxy(NIL,0,0) ;
  685.    TVwin_swrite(NIL,"Field-mode test\r\n"
  686.                     "Cursor should be  \031  there\r\n"
  687.                     "Enter a number:     Enter a string:\r\n"
  688.                     "Forced uppercase string:        With value: \r\n"
  689.                     "Press RETURN when done, or click here: ") ;
  690.    fields = TVfld_build_header(5,F_ALLOWKBD|F_READARRAY,0x70,0x0F) ;
  691.    TVfld_build_entry(fields,1,2,16,2,18,F_FILLIN,F_NEXT|F_NUMBER|F_CLEAR,1,0) ;
  692.    TVfld_build_entry(fields,2,2,35,2,50,F_FILLIN,F_NEXT|F_CLEAR,1,0) ;
  693.    TVfld_build_entry(fields,3,3,25,3,30,F_FILLIN,F_NEXT|F_UPPER|F_CLEAR,1,0) ;
  694.    TVfld_build_entry(fields,4,3,44,3,57,F_FILLIN,F_NEXT|F_CLEAR,1,0) ;
  695.    TVfld_build_entry(fields,5,4,39,4,42,F_MENU,'\r',0x70,0) ;
  696.    TVwin_stream(NIL,fields) ;
  697.    TVfld_swrite(NIL,4,"default") ;
  698.    TVfld_attr(NIL,4,0x70) ;
  699.    TVfld_cursor(NIL,1) ;   /* position cursor in first field */
  700.    TVwin_redraw(NIL) ;
  701.    TVfld_altmode(NIL,TRUE) ;
  702.    len = TVkbd_read(NIL,buffer,sizeof(buffer)) ;
  703.    status = (int) TVkbd_status(NIL) ;
  704.    TVfld_altmode(NIL,FALSE) ;
  705.    free(fields) ;  /* now we can finally free the stream */
  706.    TVwin_gotoxy(NIL,0,6) ;
  707.    TVwin_printf(NIL,"Got '%.*s'\r\nStatus was %d\r\nPress a key....",len,buffer,status) ;
  708.    TVwin_hcur(NIL) ;
  709.    bioskey(0) ;
  710. }
  711.  
  712. /*===============================================================*/
  713.  
  714. void main()
  715. {
  716.    /* initialize everything and return DESQview version number */
  717.    if (DVinit() == 0)
  718.       {
  719.       SE_STRC("This program requires DESQview 2.0 or later\n");
  720.       exit(1);
  721.       }
  722.    stream_test() ;
  723.    TVerrormsg(0,"TEST ERROR MESSAGE - press ESC",1,0,0,0);
  724.    TVsound(500,1);
  725.    TVsound(880,2);
  726.    TVsound(440,3);
  727.    notify_test() ;
  728.    timer_test() ;
  729.    pointer_test();
  730.    field_test() ;
  731.    fork_test() ;
  732.    interrupt_test() ;
  733.    objectq_test() ;
  734.    multiwindow_test() ;
  735.    /* quit DESQview API */
  736.    DVexit() ;
  737. }
  738.  
  739. /* End of INT15DEM.C */
  740.